#污染物
import pymongo
import lib.iaqi
import lib.mongodb
import math
table = 'main_kongqi'

def getBT(title,sheets,month):
    dataList = lib.mongodb.getSearch(title,sheets,month)
    datalist = dataList['data']
    # returndata = []
    gradeList = []
    for date in datalist:
        data2 = {'title':datalist[date]['title'],'date':date,'daySO2':datalist[date]['daySO2'],'dayPM25':datalist[date]['dayPM25'],'dayNO':datalist[date]['dayNO'],'dayNO2':datalist[date]['dayNO2'],'dayNOx':datalist[date]['dayNOx'],'dayO3':datalist[date]['dayO3'],'dayCO':datalist[date]['dayCO'],'dayPM10':datalist[date]['dayPM10']}

        # data2['AQI'] = math.ceil(lib.iaqi.getAQI(data2))
        gradeList.append(lib.iaqi.grade(data2))
    data = {'优':0,'良':0,'轻度污染':0,'中度污染':0,'重度污染':0}
    for i in gradeList:
        grade = i['grade']
        if grade == '优':
            data['优'] += 1
        elif grade == '良':
            data['良'] += 1
        elif grade == '轻度污染':
            data['轻度污染'] += 1
        elif grade == '中度污染':
            data['中度污染'] += 1
        elif grade == '重度污染':
            data['重度污染'] += 1
    myclient = pymongo.MongoClient("mongodb://localhost:27017/")
    mydb = myclient[table]
    mycol = mydb[title]
    x = mycol.find_one()
    monthList = []
    avgs = x['avg']
    for types in avgs[sheets]:
        for i in avgs[sheets][types]:
            month = int(i.split('-')[1])
            if month not in monthList:
                monthList.append(month)
    return {'data':data,'monthList':monthList}
    # with open('./test_text.txt','w') as file_object:
        # file_object.write(str(data))
def getPollutant(title,sheets,time='0'):
    myclient = pymongo.MongoClient("mongodb://localhost:27017/")
    mydb = myclient[table]
    mycol = mydb[title]
    x = mycol.find_one()
    datalist = []
    sheetList = []
    avgs = x['avg']
    for sheet in avgs:
        sheetList.append(sheet)
    monthList = []
    for types in avgs[sheets]:
        for i in avgs[sheets][types]:
            month = int(i.split('-')[1])
            data = lib.mongodb.getSearch(title,sheets,month)
            if month not in monthList:
                monthList.append(month)
                datalist.append(data['data'])
    returndata = []
    for data in datalist:
        # print(i)
        for j in data:
            if(time != '0'):
                month = time.split('_')
                start_time = month[0]
                start_time = start_time.replace('-','')
                end_time = month[1]
                end_time = end_time.replace('-','')
                date = j.replace('-','')
                if int(date)>=int(start_time) and int(date) <= int(end_time):
                    data2 = {'title':data[j]['title'],'date':j,'daySO2':data[j]['daySO2'],'dayPM25':data[j]['dayPM25'],'dayNO':data[j]['dayNO'],'dayNO2':data[j]['dayNO2'],'dayNOx':data[j]['dayNOx'],'dayO3':data[j]['dayO3'],'dayCO':data[j]['dayCO'],'dayPM10':data[j]['dayPM10']}
                    data2['AQI'] = math.ceil(lib.iaqi.getAQI(data2))
                    data2['AQIList'] = lib.iaqi.getAQIList(data2)
                    data2['grade'] = lib.iaqi.grade(data2)
                    data2['firstWR'] = lib.iaqi.getWRW(data2)
                    data2['CB'] = lib.iaqi.chaobiao(data2)
                    returndata.append(data2)
            else:
                data2 = {'title':data[j]['title'],'date':j,'daySO2':data[j]['daySO2'],'dayPM25':data[j]['dayPM25'],'dayNO':data[j]['dayNO'],'dayNO2':data[j]['dayNO2'],'dayNOx':data[j]['dayNOx'],'dayO3':data[j]['dayO3'],'dayCO':data[j]['dayCO'],'dayPM10':data[j]['dayPM10']}
                data2['AQI'] = math.ceil(lib.iaqi.getAQI(data2))
                data2['AQIList'] = lib.iaqi.getAQIList(data2)
                data2['grade'] = lib.iaqi.grade(data2)
                data2['firstWR'] = lib.iaqi.getWRW(data2)
                data2['CB'] = lib.iaqi.chaobiao(data2)
                returndata.append(data2)
    return {'returndata':returndata,'sheetList':sheetList,'monthList':monthList}